﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;


namespace DA1
{
    public partial class PhieuGiaoHang : Form
    {
        public PhieuGiaoHang()
        {
            InitializeComponent();
        }
        private void PhieuGiaoHang_Load(object sender, EventArgs e)
        {
            PhieuGiao_BUS bus_KH = new PhieuGiao_BUS();
            cmb_KhachHang.DataSource = bus_KH.GetDanhSachKH();
            cmb_KhachHang.DisplayMember = "TenKH";
            cmb_KhachHang.ValueMember = "MaKH";
        }
        private void btn_GiaoNL_Click(object sender, EventArgs e)
        {
            PhieuGiao_BUS bus = new PhieuGiao_BUS();
            DGV_PhieuGiaoNL.Visible = true;
            DGV_PhieuGiaoTP.Visible = false;
            DGV_PhieuGiaoNL.DataSource = bus.GetDanhSachNL();          
        }

        private void Giao_TP_Click(object sender, EventArgs e)
        {
            PhieuGiao_BUS bus = new PhieuGiao_BUS();
            DGV_PhieuGiaoTP.Visible = true;
            DGV_PhieuGiaoNL.Visible = false;
            DGV_PhieuGiaoTP.DataSource = bus.GetDanhSachTP();
        }

        private void btn_LapPhieu_Click(object sender, EventArgs e)
        {
            PhieuGiao_BUS bus = new PhieuGiao_BUS();
            PhieuGiao_DTO dto = new PhieuGiao_DTO();
            try
            {
                dto.MaKH = int.Parse(cmb_KhachHang.SelectedValue.ToString());
                dto.NgayGiao = DateTime.Parse(DTPK_NgayGiao.Text.ToString());
                dto.TienNo = int.Parse(txt_TienNo.Text.ToString());
                dto.TienTra = int.Parse(txt_SoTienTra.Text.ToString());
                dto.ThanhTien = int.Parse(txt_ThanhTien.Text.ToString());
                dto.MaPhieuGiao = bus.proc_Ins_PhieuGiao(dto);
                txt_MaPhieuGiao.Text = dto.MaPhieuGiao.ToString();
            }
            catch
            {
                Lbl_ThongBao.Text = "Chua tao duoc thong tin chung cua PhieuGiao";
                Lbl_ThongBao.Visible = true;
            }
            //Insert CT ve NL hoac thanh pham
            try
            {
                if (DGV_PhieuGiaoNL.Visible == true)// Ins PhieuGiaoNL
                {
                    for (int i = 0; i < DGV_PhieuGiaoNL.Rows.Count; i++)
                    {
                        if (DGV_PhieuGiaoNL.Rows[i].Cells["Check_NLG"].Value == null)
                            DGV_PhieuGiaoNL.Rows[i].Cells["Check_NLG"].Value = "false";
                        bool CheckBox = bool.Parse(DGV_PhieuGiaoNL.Rows[i].Cells["Check_NLG"].Value.ToString());
                        if (CheckBox == true)
                        {
                            dto.MaNLG = int.Parse(DGV_PhieuGiaoNL.Rows[i].Cells["PGNL_MaNLG"].Value.ToString());
                            if (DGV_PhieuGiaoNL.Rows[i].Cells["PGNL_SoLuong"].Value == null)
                            {
                                Lbl_ThongBao.Text = "Chua nhap So Luong";
                                Lbl_ThongBao.Visible = true;
                                return;
                            }
                            dto.SoLuong = int.Parse(DGV_PhieuGiaoNL.Rows[i].Cells["PGNL_SoLuong"].Value.ToString());
                            dto.DonGia = int.Parse(DGV_PhieuGiaoNL.Rows[i].Cells["PGNL_DonGia"].Value.ToString());
                            bus.proc_Ins_CT_PhieuGiaoNL(dto);
                        }
                    }
                    Lbl_ThongBao.Text = "Lap Phieu Giao NL thanh cong";
                    Lbl_ThongBao.Visible = true;
                }
            }
            catch
            {
                Lbl_ThongBao.Text = "Chua tao duoc PhieuGiaoNL";
                Lbl_ThongBao.Visible = true;
            }
            try
            {
                if (DGV_PhieuGiaoTP.Visible == true)// Ins PhieuGiaoTP 
                {
                    for (int i = 0; i < DGV_PhieuGiaoTP.Rows.Count; i++)
                    {
                        if (DGV_PhieuGiaoTP.Rows[i].Cells["Check_TP"].Value == null)
                            DGV_PhieuGiaoTP.Rows[i].Cells["Check_TP"].Value = "false";
                        bool CheckBox = bool.Parse(DGV_PhieuGiaoTP.Rows[i].Cells["Check_TP"].Value.ToString());
                        if (CheckBox == true)
                        {
                            dto.MaTP = int.Parse(DGV_PhieuGiaoTP.Rows[i].Cells["PGTP_MaTP"].Value.ToString());
                            if (DGV_PhieuGiaoTP.Rows[i].Cells["PGTP_SoLuong"].Value == null)
                            {
                                Lbl_ThongBao.Text = "Chua nhap So Luong";
                                Lbl_ThongBao.Visible = true;
                                return;
                            }
                            dto.SoLuong = int.Parse(DGV_PhieuGiaoTP.Rows[i].Cells["PGTP_SoLuong"].Value.ToString());
                            dto.DonGia = int.Parse(DGV_PhieuGiaoTP.Rows[i].Cells["PGTP_DonGia"].Value.ToString());
                            bus.proc_Ins_CT_PhieuGiaoTP(dto);
                        }
                    }
                    Lbl_ThongBao.Text = "Lap Phieu Giao TP thanh cong";
                    Lbl_ThongBao.Visible = true;
                }
            }
            catch
            {
                Lbl_ThongBao.Text = "Chua tao duoc PhieuGiaoTP";
                Lbl_ThongBao.Visible = true;
            }
        }

        private void cmb_KhachHang_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                PhieuGiao_BUS bus = new PhieuGiao_BUS();
                Object dt = bus.GetDiaChiTheoMaKH(int.Parse(cmb_KhachHang.SelectedValue.ToString()));
                txt_DiaChiGiao.Text = bus.GetDiaChiTheoMaKH(int.Parse(cmb_KhachHang.SelectedValue.ToString())).ToString();
            }
            catch 
            {
                Lbl_ThongBao.Text = "Loi rui";
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        
    }
}

